package org.jeecg.modules.friends.friendsinfo.controller;

import java.util.Objects;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.ControllerMethodCheck;
import org.jeecg.common.aspect.annotation.AutoLog;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.exception.MakeFriendsBaseException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.friendsinfo.dto.ApplyResultDTO;
import org.jeecg.modules.friends.friendsinfo.dto.FriendsInfoDTO;
import org.jeecg.modules.friends.friendsinfo.entity.FriendsInfo;
import org.jeecg.modules.friends.friendsinfo.param.FriendsListParam;
import org.jeecg.modules.friends.friendsinfo.param.MakeFriendsParam;
import org.jeecg.modules.friends.friendsinfo.param.OperateApplyParam;
import org.jeecg.modules.friends.friendsinfo.service.IFriendsInfoService;
import org.jeecg.modules.friends.service.impl.LCD;
import org.jeecg.modules.friends.store.mapper.BusinessDictionaryMapper;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 好友信息表
 * @Author: jeecg-boot
 * @Date: 2024-05-15
 * @Version: V1.0
 */
@Slf4j
@RestController
@Api(tags = "好友信息接口")
@RequestMapping("/friends/friendsInfo")
public class FriendsInfoController extends JeecgController<FriendsInfo, IFriendsInfoService> {
    @Autowired
    private IFriendsInfoService friendsInfoService;

    @Autowired
    private IUserService userService;


    @ApiOperation(value = "好友信息-分页列表查询", notes = "好友信息-分页列表查询")
    @PostMapping(value = "/friendsList")
    @ControllerMethodCheck
    public Result<Page<FriendsInfoDTO>> queryPageList(@RequestBody FriendsListParam param,
            @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        log.info("req:{} queryPageList#userId:{},", JSON.toJSONString(param), user.getId());
        Page<FriendsInfoDTO> friendsInfoDTOS = friendsInfoService.friendsList(param, user.getId());


        return Result.OK(friendsInfoDTOS);
    }

    /**
     * 添加
     */
    @ApiOperation(value = "添加好友", notes = "好友信息-添加")
    @PostMapping(value = "/applyFriend")
    @ControllerMethodCheck
    public Result<ApplyResultDTO> applyFriend(@RequestBody MakeFriendsParam makeFriendsParam,
            @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        if (user.getId().equals(makeFriendsParam.getToUserId())) {
            return Result.error("不能添加自己");
        }
        User toUser = userService.findById(makeFriendsParam.getToUserId());
        if (toUser == null) {
            throw new MakeFriendsBaseException("用户不存在");
        }
        ApplyResultDTO applyResultDTO = friendsInfoService.addFriends(makeFriendsParam, user.getId());
        if (StringUtils.isNotEmpty(applyResultDTO.getSelfUserHeadPic())) {
            Result<ApplyResultDTO> resultDTOResult = new Result<>();
            resultDTOResult.setCode(201);
            resultDTOResult.setResult(applyResultDTO);
            resultDTOResult.setSuccess(true);
            resultDTOResult.setTimestamp(System.currentTimeMillis());
            return resultDTOResult;
        }
        return Result.OK("添加成功！");
    }

    @ApiOperation(value = "Pass用户", notes = "Pass用户")
    @PostMapping(value = "/passUser")
    @ControllerMethodCheck
    public Result<?> passUser(@RequestBody MakeFriendsParam makeFriendsParam, @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        User toUser = userService.findById(makeFriendsParam.getToUserId());
        if (toUser == null) {
            throw new MakeFriendsBaseException("用户不存在");
        }
        friendsInfoService.passUser(makeFriendsParam, user.getId());
        return Result.OK("pass用户！");
    }

    @ApiOperation(value = "Pass用户", notes = "Pass用户")
    @PostMapping(value = "/passUser/v2")
    @ControllerMethodCheck
    public Result<Boolean> passUserV2(@RequestBody MakeFriendsParam makeFriendsParam, @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        User toUser = userService.findById(makeFriendsParam.getToUserId());
        if (toUser == null) {
            throw new MakeFriendsBaseException("用户不存在");
        }
        friendsInfoService.passUser(makeFriendsParam, user.getId());
        long count = friendsInfoService.queryFriendsCountToday(user.getId());
        String maxTimes = LCD.getContentDefault(1, "11");
        if (count >= Long.parseLong(maxTimes)) {
            return Result.OK(false);
        }
        return Result.OK(true);
    }


    @AutoLog(value = "操作申请")
    @ApiOperation(value = "操作申请", notes = "操作申请")
    @PostMapping(value = "/operateApply")
    public Result<?> addFriends(@RequestBody OperateApplyParam operateApplyParam, @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        if (Objects.isNull(operateApplyParam.getId())) {
            return Result.error("参数错误");
        }
        FriendsInfo friendsInfo = friendsInfoService.getById(operateApplyParam.getId());
        if (Objects.isNull(friendsInfo)) {
            return Result.error("申请记录不存在");
        }
        if (friendsInfo.getToUserId().longValue() != user.getId().longValue()) {
            return Result.error("无权限操作");
        }
        Boolean flag = friendsInfoService.operateApply(operateApplyParam, user.getId());
        if (flag) {
            return Result.OK("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    private User getUser(String token) {
        String mobile = JwtUtil.getMobile(token);
        User user = userService.getUserByPhone(mobile);
        return user;
    }
}
